#include "XFtpPORT.h"
#include "LogWrapper.h"
#include "XFtpUtils.h"
#include "XFtpSession.h"

XFtpPORT::XFtpPORT()
{
}


XFtpPORT::~XFtpPORT()
{
}

void XFtpPORT::parse(const std::string &type, const std::string &msg)
{
    //PORT n1,n2,n3,n4,n5,n6\r\n
    //port = n5*256 + n6
    std::string respMsg;
    LOG_TRACE("type {0} msg {1}", type, msg);
    do {
        auto key = XFtpUtils::split(msg, ",");
        if (key.size() != 6) {
            LOG_WARN("PORT msg({0})format wrong", msg);
            respMsg = "501 Syntax error in parameters or arguments.\r\n";
            break;
        }
        std::string ip = key[0] + "." + key[1] + "." + key[2] + "." + key[3];
        unsigned short port = atoi(key[4].c_str()) * 256 + atoi(key[5].c_str());
        mSesssion->setDataChannelInfo(ip, port);
        LOG_INFO("datachannel ip: {0} port: {1}", ip, port);
        respMsg = "200 PORT Command Successful.\r\n";
    } while (0);
    responseFtpCmd(respMsg);
}
